<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>要素右键菜单</title>
    <style type="text/css">
        body{
            margin: 0;
            overflow: hidden;
            background: #fff;
        }

        #toolbar {
            position: relative;
            padding-top: 5px;
            padding-bottom: 10px;
        }
        #map{
            position: relative;
            height: 510px;
            border:1px solid #3473b7;
        }
        #myMenu{
            position: absolute;
            background-color: silver;
            visibility: hidden;
        }
        ul{
            list-style-type:none;
            width:112px;
        }
        #myMenu ul{
            float:left;
            border:1px solid #979797;
            background:#f1f1f1 url(../examples/images/line.png) 36px 0 repeat-y;
            padding:2px;
            box-shadow:2px 2px 2px rgba(0,0,0,.6);}
        #myMenu ul li{
            width:112px;
            float:left;
            clear:both;
            height:35px;
            cursor:pointer;
            line-height:32px;
        }
        #myMenu ul li:hover{
            background-color: #CAE1FF;
        }
    </style>
    <link href='./css/bootstrap.min.css' rel='stylesheet' />
    <link href='./css/bootstrap-responsive.min.css' rel='stylesheet' />
    <script src = '../libs/SuperMap.Include.js'></script>
    <script type="text/javascript">
        var map,layer,vectorLayer, gFeature,pointVector,
            host = document.location.toString().match(/file:\/\//) ? "http://localhost:8090" : 'http://' + document.location.host;
        url = host + "/iserver/services/map-world/rest/maps/World";
        function init(){
            var broz = SuperMap.Util.getBrowser();
            if (broz.device === 'android'|| broz.device === 'apple') {
                alert('您的设备不支持，请使用pc或其他设备');
                return;
            }
            map = new SuperMap.Map("map",{
                eventListeners:{"movestart":function(){
                    menu.style.visibility="hidden";
                },
                    "click":function(){
                        menu.style.visibility="hidden";
                    }}
            });
            layer = new SuperMap.Layer.TiledDynamicRESTLayer("world",url,{transparent: true, cacheEnabled:true}, {maxResolution:"auto"});
            layer.events.on({"layerInitialized":addLayer});
            vectorLayer= new SuperMap.Layer.Vector("vectorlayer");
            addData();
            var callbacks={
                rightclick: creatMenu
            };
            var  selectFeature = new SuperMap.Control.SelectFeature(vectorLayer,
                    {
                        callbacks: callbacks
                    });
            map.addControl(selectFeature);
            selectFeature.activate();
        }
        function addLayer(){
            map.addLayers([layer,vectorLayer]);
            map.setCenter(new SuperMap.LonLat(0,0),0);
        }
        //创建EventUtil对象
        var EventUtil={
            addHandler:function(element,type,handler){
                if(element.addEventListener){
                    element.addEventListener(type,handler,false);
                }
                else if(element.attachEvent){
                    element.attachEvent("on"+type,handler);
                }
            },
            getEvent:function(event){
                return event?event:window.event;
            },
            //取消事件的默认行为
            preventDefault:function(event){
                if(event.preventDefault){
                    event.preventDefault();
                }else{
                    event.returnValue= false;
                }
            }
        };
        EventUtil.addHandler(window,"load",function(event){
            menu= document.getElementById("myMenu");
            EventUtil.addHandler(myMenu,"contextmenu",function(event){
                event= EventUtil.getEvent(event);
                EventUtil.preventDefault(event);
                menu.style.visibility="visible";
            });
            EventUtil.addHandler(document,"click",function(event){
                menu.style.visibility="hidden";
            });
        });
        function creatMenu(currentFeature){
            var centerPoint= currentFeature.geometry.getCentroid();
            var pos= new SuperMap.LonLat(centerPoint.x,centerPoint.y);
            var p=map.getPixelFromLonLat(pos);
            menu.style.left= p.x+"px";
            menu.style.top= p.y+50+"px";
            menu.style.visibility="visible";
            gFeature=currentFeature;

        }
        function addData(){
            vectorLayer.removeFeatures(pointVector);
            //点
            var point= new SuperMap.Geometry.Point(0,0);
             pointVector = new SuperMap.Feature.Vector(point);
            pointVector.style={
                fillColor:"red",
                strokeColor:"yellow",
                pointRadius:10
            };
            vectorLayer.addFeatures([pointVector]);
        }
        function setCenter(){
            var centerP= gFeature.geometry.getCentroid();
            menu.style.visibility="hidden";
            map.setCenter(new SuperMap.LonLat(centerP.x,centerP.y),0);
        }
        function deleteSelect(){
            menu.style.visibility="hidden";
            vectorLayer.removeFeatures(gFeature);


        }
    </script>
</head>
<body onload="init()">
<div id="toolbar">
    <input type="button" class="btn" value="加载数据" onclick="addData()" />
    <label id="lab"></label>
</div>
<div id="map"></div>
<div id="myMenu">
    <ul style="margin-top: 0px; margin-bottom: 0px;margin-left: 0px" >
        <li onclick="setCenter()" >
            <div style="float:left;padding-right: 5px;width:30px;height:30px"><img src="../examples/images/center.png" /></div>
            <div style="float:left;width:52px;height:30px;  text-align: center; font-size: 15px;">居中</div>  </li>
        <li  onclick="deleteSelect()">
            <div style="float:left;padding-right: 5px;width:30px;height:30px"><img src="../examples/images/delete.png" /></div>
            <div style="float:left;width:52px;height:30px;  text-align: center; font-size: 15px;">删除</div>  </li>
        </li>
    </ul>
</div>
</body>
</html>